package com.yxhl.zoume.common.presenter;

import android.content.Context;

import com.yxhl.protobuf.ClientType;
import com.yxhl.protobuf.CommonReqParams;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.data.http.model.account.ZMAccount;
import com.yxhl.zoume.utils.AccountUtils;
import com.yxhl.zoume.utils.AppInfoUtils;
import com.yxhl.zoume.utils.NetworkUtils;
import com.yxhl.zoume.utils.PromptUtils;

import javax.inject.Inject;

import rx.Subscription;

/**
 * Created by sugarya on 16/4/25.
 */
public class BasePresenter implements IPresenter {

    protected static final String CLIENT_SYSTEM = "Android";
    private static final String TAG = "BasePresenter";
    protected Context mContext;
    protected Subscription mSubscription;
    private String mobile;
    private String token;

    @Inject
    public BasePresenter(Context context) {
        mContext = context;
    }


    protected String getMobile() {
        ZMAccount account = AccountUtils.getInstance(mContext).getAccount();
        if (account != null) {
            mobile = account.getAccountName();
        } else {
            mobile = "";
        }
        return mobile;
    }


    protected String getToken() {
        ZMAccount account = AccountUtils.getInstance(mContext).getAccount();
        if (account != null) {
            token = account.getAccessToken();
        } else {
            token = "";
        }
        return token;
    }

    /**
     * 获取当前版本名
     *
     * @return
     */
    protected String getClientVersionName() {
        return AppInfoUtils.getVersionName();
    }

    /**
     * 获取当前版本编号
     *
     * @return
     */
    protected String getClientVersionCode() {
        return AppInfoUtils.getVersionCode();
    }


    /**
     * 获取 CommonReqParams
     *
     * @return
     */
    protected CommonReqParams getCommonReqParams() {
        return CommonReqParams
                .newBuilder()
                .setClientType(ClientType.USER)
                .setClientSystem(CLIENT_SYSTEM)
                .setClientVersion(getClientVersionName())
                .setMobile(getMobile())
                .setToken(getToken())
                .build();
    }


    @Override
    public void onPause() {
    }

    @Override
    public void onResume() {
        if (!NetworkUtils.getInstance(mContext).isConnected()) {
            PromptUtils.showShort(mContext, mContext.getString(R.string.network_not_available));
        }
    }


    @Override
    public void onDestroy() {
        if (mSubscription != null) {
            mSubscription.unsubscribe();
        }
    }

    @Override
    public void attachView(BaseView view) {

    }
}
